<template>
	<view class="page_body">
		<!-- 状态栏高度 设置背景色 占位状态栏-->
		<view class="ztl"
			:style="{'height':statusBarHeight}"></view>
		<!-- 占位盒子 -->
		<view class=""
			:style="{'height': hieght}"></view>
		<!-- 导航栏 -->
		<navBar bgColor="#FFF">
			<image :slot="'left'"
				@click="navToBack"
				:src="xcxImgUrl && xcxImgUrl+'fanhui.svg'"
				style="width: 36rpx;height: 36rpx;"></image>
			<text :slot="'content'">收货地址</text>
		</navBar>
		
		<scroll-view 
		class="main_harvestAddress pd_lr10" 
		scroll-y
		@scrolltolower="scrolltolower"
		:style="{'height': 'calc(100vh - ' + demoTopHieght +')'}">
			<view class="main_addressList mt10" v-for="(item,index) in addressList" :key="index">
				<view 
				 @click="selectAddress(item,index)"
				 class="main_addressList_item pd10">
					<image v-if="item.type == 1" class="morendizhi" :src="xcxImgUrl && xcxImgUrl+'page_public/static/morendizhi.svg'"></image>
					<view v-if="item.type == 1" class="morendizhi">
						<text  >默认地址</text>
					</view>
					<view class="main_addressList_item_top">
						<view class="main_addressList_item_top_zi">
							<view>{{item.name ? item.name.substr(0,1) : '默'}}</view>
						</view>
						<view class="main_addressList_item_top_cn ml5">
							<view class="main_addressList_item_top_cn_top">
								<text class="cn_top_name">{{item.name ? item.name : ''}}</text>
								<text class="cn_top_phone ml5">{{item.phone ? item.phone : ''}}</text>
							</view>
							<view class="main_addressList_item_top_cn_btn mt10 texttowShow">
								 {{item.province ? item.province : ''}}
								 {{item.city ? item.city : ''}}
								 {{item.area ? item.area : '' }}
								 {{item.address ? item.address : '' }}
							</view>
						</view>
						<view class="checkbox">
							<!-- <checkbox
							color="#ec4141" 
							style="transform:scale(0.7)" 
							:checked="index == addressIndex ? true : false"/> -->
							<!-- <view class="custom_checkbox" :class="{active : (index == addressIndex ? true : false)}">
								<u-checkbox-group :value="item.checked" @change="checkrdAddress($event,index)">
								<u-checkbox
									:name="true"></u-checkbox>
								</u-checkbox-group>
							</view> -->
						</view>
					</view>
				</view>
			</view>
		</scroll-view>
		
		<view class="main_btn" @click="addAddress">
			<newBtn name="管理地址" bg-color="#F33340" color="#FFF" ></newBtn>
		</view>
		
		
		
		</u-modal>
	</view>
</template>

<script>
	import {mapGetters} from 'vuex'
	import newBtn from '@/components/newBtn/newBtn.vue'
	import {listMallCustomerAddressPageInfo,
	setDefaultMallCustomerAddress
	} from '@/api/setting.js'
	export default {
		computed: {
			...mapGetters(['demoTopHieght']),
		},
		components:{
			newBtn
		},
		data() {
			return {
				statusBarHeight:'',
				hieght:'',
				pageInfo:{//分页信息
					currentPage:1,//当前页码[默认为1]
					pageSize:10,//每页显示条数[默认为20]
					sort:1
				},
				pages:'',//总页数
				addressList:[],//地址列表
				addressIndex:'',//当前选择地址下标
				addressItem:{},//当前选择的地址对象
				type:2,//确认订单页面的状态
			};
		},
		onShow() {
			this.$nextTick(() => {
				// 获取状态栏的高度
				this.statusBarHeight = this.$store.state.statusBarHeight * 2 + 'rpx'
				// 计算站位的高度
				this.hieght = (this.$store.state.navBarHeight) * 2 + 'rpx'
				//获取高度
				this.Utils.getDemoTopHeight('main_harvestAddress')
			})
			this.getList()
		},
		onLoad(options) {
			this.type = options?.type || 2
			//获取列表
			this.getList()
		},
		methods:{
			checkrdAddress(e,index){
				// this.addressList[index].type = 1;
				this.addressList[index].checked = e;
				this.addressIndex = index;
			},
			//返回上一级
			navToBack(){
				// uni.navigateTo({
				// 	url:`/page_public/confirmOrder?type=${this.type}`
				// })
				uni.navigateBack()
			},
			//去管理地址
			addAddress(){
				uni.navigateTo({
					url:'/page_setting/harvestAddress?type=address'
				})
			},
			//分页加载
			scrolltolower(){
				++this.pageInfo.currentPage
				if(this.pageInfo.currentPage > this.pages){
					this.Utils.toast('数据加载完啦')
					return
				}
				this.getList()
			},
			//获取收货地址
			async getList(){
				uni.showLoading()
				let res = await listMallCustomerAddressPageInfo(this.pageInfo)
				console.log(res);
				if(res.code == 2000){
					this.addressIndex = res?.data?.info?.list.findIndex(v => v.type == 1);
					this.pages = res.data && 
					res.data.info && 
					res.data.info.pages || 0;
					uni.hideLoading()
					if(this.pageInfo.currentPage == 1){
						this.addressList = res.data &&
						res.data.info && 
						res.data.info.list && 
						res.data.info.list.length ? 
						res.data.info.list.map(v => {
							if(v.type == 1){
								v['checked'] = true;
							}else{
								v['checked'] = false;
							}
							return v;
						}) : [];
					}else{
						this.addressList = res.data &&
						res.data.info && 
						res.data.info.list && 
						res.data.info.list.length ? 
						this.addressList.concat(res.data.info.list.map(v => {
							if(v.type == 1){
								v['checked'] = true;
							}else{
								v['checked'] = false;
							}
							return v;
						})) : [];
					}
				}else{
					this.Utils.toast(`${res.msg}`)
				}
			},
			//选择地址
			selectAddress(item,index){
				this.addressIndex = index
				let pages = getCurrentPages();
				let prev = pages[pages.length-2];
				prev.$vm.type = this.type;
				prev.$vm.addressInfo = item;
				uni.navigateBack({
					// url:`/page_public/confirmOrder?type=${this.type}&address=${JSON.stringify(item)}`
				})
				
			},
			
		}
		
	}
</script>

<style lang="scss" scoped>
.ztl{
	background-color: #FFF;
}

.main_harvestAddress{
	width: 100%;
	box-sizing: border-box;
	padding-bottom: 120rpx;
	.main_addressList{
		width: 100%;
		height: 200rpx;
		background-color: #FFF;
		border-radius: $border_radius_4;
		display: flex;
		align-items: center;
		.main_addressList_item{
			position: relative;
			.morendizhi{
				width: 140rpx;
				height: 40rpx;
				position: absolute;
				left: 0;
				top: 20rpx;
				font-size: $font_size_12;
				color: #FFF;
				text-align: center;
				line-height: 40rpx;
				image{
					width: 100%;
					height: 100%;
				}
			}
			
			display: flex;
			align-items: center;
			justify-content: space-between;
			width: 100%;
			height: 100%;
			.main_addressList_item_top{
				width: 100%;
				height: 100%;
				display: flex;
				margin-top: 43px;
				justify-content: space-between;
				.main_addressList_item_top_zi{
					width: 80rpx;
					height: 80rpx;
					border-radius: 50%;
					background-color: #FFE3E3;
					color: $text_color_ec4141;
					text-align: center;
					line-height: 80rpx;
					
				}
				.main_addressList_item_top_cn{
					flex: 1;
					.main_addressList_item_top_cn_top{
						.cn_top_name{
							color: $text_color_333;
							font-size: 32rpx;
							font-weight: bold;
						}
						.cn_top_phone{
							color: $text_color_808080;
							font-size: $font_size_14;
							font-weight: bold;
						}
					}
					.main_addressList_item_top_cn_btn{
						color: $text_color_333;
						font-size: $font_size_14;
					}
				}
				.checkbox{
					width: 80rpx;
				}
			}
			
		}
		
	}
}

.main_btn{
	position: fixed;
	left: 0;
	bottom: 40rpx;
	width: 100%;
}
</style>
